/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.teaching.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.teaching.entity.Course;
import com.thinkgem.jeesite.modules.teaching.entity.Library;
import com.thinkgem.jeesite.modules.teaching.entity.Question;
import com.thinkgem.jeesite.modules.teaching.service.CourseService;
import com.thinkgem.jeesite.modules.teaching.service.LibraryService;
import com.thinkgem.jeesite.modules.teaching.service.QuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 课程管理Controller
 * @author 王卫党
 * @version 2018-01-23
 */
@Controller
@RequestMapping(value = "${adminPath}/teaching/course")
public class CourseController extends BaseController {

	@Autowired
	private CourseService courseService;

    @Autowired
    private LibraryService libraryService;

    @Autowired
    private QuestionService questionService;
	
	@ModelAttribute
	public Course get(@RequestParam(required=false) String id) {
		Course entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = courseService.get(id);
		}
		if (entity == null){
			entity = new Course();
		}
		return entity;
	}
	
	@RequiresPermissions("teaching:course:view")
	@RequestMapping(value = {"list", ""})
	public String list(Course course, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Course> page = courseService.findPage(new Page<Course>(request, response), course); 
		model.addAttribute("page", page);
		return "modules/teaching/courseList";
	}

	@RequiresPermissions("teaching:course:view")
	@RequestMapping(value = "form")
	public String form(Course course, Model model) {
		model.addAttribute("teaching", course);
		return "modules/teaching/courseForm";
	}

	@RequiresPermissions("teaching:course:edit")
	@RequestMapping(value = "save")
	public String save(Course course, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, course)){
			return form(course, model);
		}
		courseService.save(course);
		addMessage(redirectAttributes, "保存课程成功");
		return "redirect:"+Global.getAdminPath()+"/teaching/course/?repage";
	}
	
	@RequiresPermissions("teaching:course:edit")
	@RequestMapping(value = "delete")
	public String delete(Course course, RedirectAttributes redirectAttributes) {
		courseService.delete(course);
		addMessage(redirectAttributes, "删除课程成功");
		return "redirect:"+Global.getAdminPath()+"/teaching/course/?repage";
	}

    @ModelAttribute
    public Library getLibrary(@RequestParam(required=false) String id) {
        Library entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = libraryService.get(id);
        }
        if (entity == null){
            entity = new Library();
        }
        return entity;
    }

    @RequiresPermissions("teaching:course:view")
    @RequestMapping(value = {"libraries"})
    public String listLibraries(Library library, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Library> page = libraryService.findPage(new Page<Library>(request, response), library);
        model.addAttribute("page", page);
        return "modules/teaching/libraryList";
    }

    @RequiresPermissions("teaching:course:view")
    @RequestMapping(value = "library/form")
    public String formLibrary(Library library, Model model) {
        model.addAttribute("library", library);
        return "modules/teaching/libraryForm";
    }

    @RequiresPermissions("teaching:course:edit")
    @RequestMapping(value = "library/save")
    public String saveLibrary(Library library, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, library)){
            return formLibrary(library, model);
        }
        libraryService.save(library);
        addMessage(redirectAttributes, "保存题库（专题）成功");
        return "redirect:"+Global.getAdminPath()+"/teaching/course/libraries/?repage";
    }

    @RequiresPermissions("teaching:course:edit")
    @RequestMapping(value = "library/delete")
    public String deleteLibrary(Library library, RedirectAttributes redirectAttributes) {
        libraryService.delete(library);
        addMessage(redirectAttributes, "删除题库（专题）成功");
        return "redirect:"+Global.getAdminPath()+"/teaching/course/libraries/?repage";
    }

    @RequiresPermissions("teaching:course:view")
    @RequestMapping(value = {"library/questions"})
    public String listQuestions(Question question,
                                HttpServletRequest request,
                                HttpServletResponse response,
                                Model model) {
        Library library = getLibrary(question.getLibraryId());
        model.addAttribute("library", library);

        Page<Question> pageParameter = new Page<Question>(request, response, -1);
        Page<Question> page = questionService.findPage(pageParameter, question);
        model.addAttribute("page", page);
        return "modules/teaching/questionList";
    }
}